//
//  WYStatusOriginalView.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/14.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit

import SnapKit

import SDWebImage



class WYStatusOriginalView: UIView {
    
    //记录原创微博底部的约束
    var originalViewBottpm: Constraint?
    
    //闭包  控件赋值
    var statusViewModel: WYStatusViewModel? {
        
        didSet {
        
            //判断是否为 nil
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                
                headImageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
                
            }
            //昵称
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
//            timeLabel.text = "刚刚"
            timeLabel.text = statusViewModel?.timeStr
            //设置资源内容
            sourceLabel.text = statusViewModel?.sourceContent
            //设置微博内容
            contentlabel.text = statusViewModel?.status?.text
            //等级照片
            mbrankImageview.image = statusViewModel?.mbrandImage
            //设置认证等级图片
            verifieldtypeImageView.image = statusViewModel?.verifiedImageView
            //卸载约束 
            originalViewBottpm?.uninstall()
            //判断原创微博是否有配图
            if let count = statusViewModel?.status?.pic_urls?.count where count > 0 {
            
                //有配图 设置数据源 跟新约束 显示出来 
                pictureView.picUrls = statusViewModel?.status?.pic_urls
                //显示出来
                pictureView.hidden = false
                //跟新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottpm = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
            } else {
                //不显示
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottpm = make.bottom.equalTo(contentlabel).offset(HomeCellMargin).constraint
                })

            }
            
            
        }
      
    }
    
    
    //MARK -- 懒加载控件
    //头像
    private lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证等级的图片
    private lazy var verifieldtypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //会员等级
    private lazy var mbrankImageview: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //昵称
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.grayColor(), fontSize: 12)
    //发表日期
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.grayColor(), fontSize: 12)
    //来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
    //微博内容
    private lazy var contentlabel: UILabel = {
    
        let label = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
        label.numberOfLines = 0
        return label
    }()
    //配图
    private lazy var pictureView: WYStatusPicView = {
        
        let view = WYStatusPicView()
        view.backgroundColor = self.backgroundColor
        return view
        
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        
       setUPUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加视图
    private func setUPUI() {
    
        //添加到视图上
        addSubview(headImageView)
        addSubview(verifieldtypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageview)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentlabel)
        addSubview(pictureView)
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifieldtypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_top)
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
        }
        mbrankImageview.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView.snp_bottom)
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.leading.equalTo(timeLabel.snp_trailing).offset(HomeCellMargin)
        }
        contentlabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_bottom).offset(HomeCellMargin)
            make.leading.equalTo(timeLabel.snp_leading)
            make.trailing.equalTo(self).offset(-HomeCellMargin)//距离右边有一个间距
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self.snp_leading).offset(HomeCellMargin)
            make.top.equalTo(contentlabel.snp_bottom).offset(HomeCellMargin)
//            make.trailing.equalTo(contentlabel)
        }
        
        //下面还撑称一下  也就是说要给一个和下面相当的位置
        self.snp_makeConstraints { (make) -> Void in
          self.originalViewBottpm = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
    }
}
